/**
 * 
 */
package ltu.m7011e.server.dao.jpa;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

import com.google.appengine.api.datastore.Key;
import com.google.gwt.core.client.GWT;

import ltu.m7011e.server.dao.ILinkDao;
import ltu.m7011e.server.pojo.LinkEntity;
import ltu.m7011e.server.pojo.UserLike;
import ltu.m7011e.server.tool.EMF;

/**
 * @author robinelmouahidine
 *
 */
public class LinkDao implements ILinkDao {



	private static EntityManager em;

	/**
	 * Add the link specified in parameter in the datastore
	 * @param newLink : Link to persist
	 * 
	 */
	public void postLinkDao (LinkEntity newLink){
		em = EMF.get().createEntityManager();

		try {
			em.persist(newLink);
		} finally {
			em.close();
		}
	}
	/**
	 * @param linkId : id to the link to like
	 */
	public void addTheLikeDao(Key linkKey){
		em = EMF.get().createEntityManager();

		Query query = em.createQuery("SELECT x FROM LinkEntity x WHERE x.key = ?1");
		query.setParameter(1, linkKey);
		LinkEntity results = (LinkEntity) (query.getSingleResult());

		em.getTransaction().begin();
		results.setLikes(results.getLikes() + 1);
		em.getTransaction().commit();	

	}
	/**
	 * @param newLike
	 * @param userId
	 * @param likeLinkId
	 */
	public boolean addLikeDao(Key linkKey ,String googleUserId){
		em = EMF.get().createEntityManager();
		boolean state = false;

		try {

			Query checkquery = em.createQuery("SELECT x FROM UserLike x WHERE x.likeLinkId = ?1 AND x.userId =?2");
			checkquery.setParameter(1, linkKey.toString());
			checkquery.setParameter(2, googleUserId);

			if (checkquery.getResultList().isEmpty()){
				addTheLikeDao(linkKey);
				em.persist(new UserLike(googleUserId,linkKey.toString()));
				GWT.log("Add like - Sucess : "+googleUserId +" "+ linkKey);
				state = true;
			}

		} 

		finally {
			em.close();
		}

		return state; 
	}

	public List<LinkEntity> getLinkById(Key linkKey) {
		em = EMF.get().createEntityManager();

		Query query = em.createQuery("SELECT x FROM LinkEntity x WHERE x.key = ?1");
		query.setParameter(1, linkKey);
		List<LinkEntity> results = (query.getResultList());
		return results;


	}



	//Get links in order of date

	public List<LinkEntity> getNewestLinks(int numberOfRecords,int firstResult) {
		em = EMF.get().createEntityManager();  
		Query query = em.createQuery("SELECT x FROM LinkEntity x ORDER BY x.postDate DESC");
		@SuppressWarnings("unchecked")
		List<LinkEntity> results = (query.setFirstResult(firstResult).setMaxResults(numberOfRecords).getResultList());
		return results;
	}

	//Get links in order of likes

	public List<LinkEntity> getTopLinks(int numberOfRecords,int firstResult) {
		em = EMF.get().createEntityManager();

		Query query = em.createQuery("SELECT x FROM LinkEntity x ORDER BY x.likes DESC");
		@SuppressWarnings("unchecked")
		List<LinkEntity> results = (query.setFirstResult(firstResult).setMaxResults(numberOfRecords).getResultList());

		return results;
	}

	//Get links in order of date from specific category

	public List<LinkEntity> getNewestLinksByCat(int numberOfRecords,int firstResult,String linkCat) {
		em = EMF.get().createEntityManager();

		Query query = em.createQuery("SELECT x FROM LinkEntity x WHERE x.linkCat = ?1 ORDER BY x.postDate DESC");
		query.setParameter(1, linkCat);
		@SuppressWarnings("unchecked")
		List<LinkEntity> results = (query.setFirstResult(firstResult).setMaxResults(numberOfRecords).getResultList());
		return results;
	}


}